﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ext.Net;
using System.Web.Security;
using Viewer.BusinessObject.BusinessEntities;

namespace Viewer
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!X.IsAjaxRequest)
            {
               // this.ResourceManager1.DirectEventUrl = this.Request.Url.AbsoluteUri;
                btnUserName.Text = Context.User.Identity.Name;
                //SetUp Them
                int checkedMenuItemId = 0;
                if (this.Session["Ext.Net.Theme"] == null)
                {
                    this.Session["Ext.Net.Theme"] = Ext.Net.Theme.Default;
                 
                }
                else
                {
                   
                    Ext.Net.Theme them = (Ext.Net.Theme)this.Session["Ext.Net.Theme"];
                    switch (them)
                    {
                        case Ext.Net.Theme.Access:
                            checkedMenuItemId = 3;
                            break;
                        case Ext.Net.Theme.Default:
                            checkedMenuItemId = 0;
                            break;
                        case Ext.Net.Theme.Gray:
                            checkedMenuItemId = 1;
                            break;
                        case Ext.Net.Theme.Slate:
                            checkedMenuItemId = 2;
                            break;
                    }
                }
                CheckMenuItem item = (CheckMenuItem)MenuSkin.Items[checkedMenuItemId];
                item.Checked = true;

                //PhanQuyenByModule
                //PhanQuyenByModule();
                CreateMenu();
            }
          
        }

        [DirectMethod]
        public string GetThemeUrl(string theme)
        {
            Theme temp = (Theme)Enum.Parse(typeof(Theme), theme);

            this.Session["Ext.Net.Theme"] = temp;

            return temp == Ext.Net.Theme.Default ? "Default" : this.MainResourceManager.GetThemeUrl(temp);
        }

        /// <summary>
        /// Tao menu dong - phan quen theo Module
        /// </summary>
        private void CreateMenu()
        {
            QUModule lstModule = new QUModule();
            //get list module lv1
            var listModuleLv1= (from m in lstModule.GetAll() 
                                where (m.ModuleLevel == 1)
                                select m).ToList();
            foreach (var moduleCha in listModuleLv1)
            {
                var oPanel = CreatePanel(moduleCha);
                var listModuleCon = (from c in lstModule.GetAll()
                                     where (c.ModuleIDCha == moduleCha.ModuleID)
                                     select c).ToList();

                var oMenuPanel = CreateMenuPanel();
                oMenuPanel.ID = "MenuPanel" + moduleCha.ModuleID;
                oPanel.Items.Add(oMenuPanel);
                foreach (var moduleCon in listModuleCon)
                {
                    if (IsView(moduleCon))
                    {
                        if (oMenuPanel.Menu.Items.Count > 0)
                        {
                            oMenuPanel.Menu.Items.Add(CreateMenuSeparator());
                        }
                        oMenuPanel.Menu.Items.Add(CreateMenuItem(moduleCon,oMenuPanel));
                    }
                }

                if (oMenuPanel.Menu.Items.Count == 0)
                {
                    oPanel.Visible = false;
                    continue;
                }
                AccordionLayout1.Items.Add(oPanel);
            }                   
        }

        /// <summary>
        /// thuc hien phan quyen theo module
        /// </summary>
        private void PhanQuyenByModule()
        {
            if (Context.User.Identity.Name.ToLower() == "admin")
            {
                //MenuNhanVien.Visible = true;
                //MenuNhanVien_Separaror.Visible = true;

                //MenuBaoCaoCaNhan.Visible = true;
                //MenuBaoCaoCaNhan_Separator.Visible = true;

                //MenuBaoCaoToChuc.Visible = true;

                //Panel1.Visible = true;
                //return;
            }
            //
            //QUModule lstModule = new QUModule();

            //foreach (var module in lstModule.GetAll())
            //{
            //    if (IsView(module))
            //    {
            //        if (MenuPanel2.Menu.Items.Count > 0)
            //        {
            //            MenuPanel2.Menu.Items.Add(CreateMenuSeparator());
            //        }
            //        MenuPanel2.Menu.Items.Add(CreateMenuItem(module));

            //    }
            //}

            //Panel1.Visible = MenuPanel2.Menu.Items.Count > 0;

           
            //List<BusinessObject.BusinessEntities.QUNhomQuyenByModule_Module> lst = Session["NhomQuyenByModule_Module "] as List<BusinessObject.BusinessEntities.QUNhomQuyenByModule_Module>;

            //foreach (var item in lst)
            //{
            //    if (item.ModuleID == 5)
            //    {
            //        MenuNhanVien.Visible = true;
            //        MenuNhanVien_Separaror.Visible = true;
            //    }
            //}
            //MenuBaoCaoCaNhan.Visible = false;
            //MenuBaoCaoCaNhan_Separator.Visible = false;

            //MenuBaoCaoToChuc.Visible = false;
        }

        /// <summary>
        /// Do logout
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MenuItem_DoLogOut(object sender, DirectEventArgs e)
        {
            Enterprise.Core.CurrentContext.ClearContextStorage();
            FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");
        }

        /// <summary>
        /// Create Menu Item
        /// </summary>
        /// <param name="pModule"></param>
        /// <param name="mnuPanel"></param>
        /// <returns></returns>
        private Ext.Net.MenuItem CreateMenuItem(QUModule pModule,MenuPanel mnuPanel)
        {
            MenuItem oMenuItem = new MenuItem();
            oMenuItem.ID = pModule.ModuleID.ToString();
            oMenuItem.Text = pModule.TenModuleFull;
            oMenuItem.IconCls = GetIconCls(pModule.IconName);
            //addTab(#{MainTab}, 'idNhanVien', 'NhanVien/NhanVien.aspx','Nhân viên','icon-user');
            oMenuItem.Listeners.Click.Handler = string.Format("addTab({0}, '{1}', '{2}','{3}','{4}',{5});", MainTab.ClientID, pModule.ModuleID.ToString(), pModule.Link, pModule.TenModuleFull, oMenuItem.IconCls, mnuPanel.ClientID);
            return oMenuItem;
        }

        /// <summary>
        /// Create MenuSeparator
        /// </summary>
        /// <returns></returns>
        private MenuSeparator CreateMenuSeparator()
        {
            MenuSeparator oMenuSeparator = new MenuSeparator();
            return oMenuSeparator;
        }

        /// <summary>
        ///kiem tra co dc view hay ko
        /// </summary>
        /// <param name="pModule"></param>
        /// <returns></returns>
        private bool IsView(QUModule pModule)
        {
            if (Session["NhomQuyenByModule_Module "] == null)
            {
                Response.Redirect("Login.aspx");
                Response.Close();
            }
            if (Context.User.Identity.Name.ToLower() == "admin")
            {
                return true;
            }
            List<BusinessObject.BusinessEntities.QUNhomQuyenByModule_Module> lstNhomQuyenByModule_Module = Session["NhomQuyenByModule_Module "] as List<BusinessObject.BusinessEntities.QUNhomQuyenByModule_Module>;
            foreach (var oNhomQuyenByModule_Module in lstNhomQuyenByModule_Module)
            {
                if (oNhomQuyenByModule_Module.ModuleID == pModule.ModuleID)
                {
                    return oNhomQuyenByModule_Module.IsView;
                }
            }
            return false;
        }

        /// <summary>
        /// Create Panel
        /// </summary>
        /// <param name="pModule"></param>
        /// <returns></returns>
        private Panel CreatePanel(QUModule pModule)
        {
            Panel oPanel = new Panel();
            oPanel.Padding =6;
            oPanel.Border = false;
            oPanel.Collapsed = true;
            oPanel.IconCls = GetIconCls(pModule.IconName);
            oPanel.Title = pModule.TenModuleFull;

            return oPanel;
        }

        /// <summary>
        /// Create Menu Panel
        /// </summary>
        /// <returns></returns>
        private MenuPanel CreateMenuPanel()
        {
            MenuPanel oMenuPanel = new MenuPanel();
            return oMenuPanel;
        }

        /// <summary>
        /// Tra ve gia tri Icon Class duoc su dung o client
        /// </summary>
        /// <param name="iconName"></param>
        /// <returns></returns>
        private string GetIconCls(string iconName)
        {
            if (string.IsNullOrEmpty(iconName))
            {
                return string.Empty;
            }
            Icon ico;
            try
            {
                ico = (Icon)Enum.Parse(typeof(Icon), iconName);
            }
            catch (Exception)
            {

                ico = Icon.None;
            }

            if (ico != Icon.None)
            {
                MainResourceManager.RegisterIcon(ico);
                //oMenuItem.IconCls = "icon-" + pModule.IconName.ToLower();
            }
            return "icon-" + iconName.ToString().ToLower(); 
        }
      
    }
}
